#include<iostream>
#include <string>
#include <queue>
using namespace std;
typedef struct node{
    char data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
typedef struct node *BTree;
void PreOrder( BTree BT );
BTree CreateBTree(char *post,char*in,int n);
int main(){
	int i,n,h;
	char post[30],in[30];	
	BTree BT;
	BT=new BTNode;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>post[i];
	for(i=0;i<n;i++)
	cin>>in[i];
	BT=	CreateBTree(post,in,n);
	cout<<"Preorder: ";
	PreOrder(BT );
	return 0;
}
BTree CreateBTree(char *post,char*in,int n)
{
	BTree s;
	char *p,r;
	int k;
	if(n<=0||n>30)return NULL;
	r=*(post+n-1);
	s=new BTNode;
	s->data=r;
	for(p=in;p<in+n;p++)
	if(*p==r)
	break;
	k=p-in;
	s->lchild=CreateBTree(post,in,k);
	s->rchild=CreateBTree(post+k,p+1,n-k-1);
	return s;
}
void PreOrder( BTree BT )
{
	static int f=0;
	if(BT!=NULL)
	{
		if(f==0)
		{
			cout<<BT->data;
			f=1;
		}
		else
		cout<<" "<<BT->data;
		PreOrder(BT->lchild);
		PreOrder(BT->rchild);
	}
}
